package com.ruoyi.web.controller.blog.admin;


import com.ruoyi.blog.service.BlogService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.ActivitysConstant;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.model.activity.Activity;
import com.ruoyi.model.auth.AuthContextHolder;
import com.ruoyi.model.blog.Blog;
import com.ruoyi.model.blog.dto.BlogDto;
import com.ruoyi.model.blog.vo.UserAndBlogVO;
import com.ruoyi.model.message.UserMessage;
import com.ruoyi.model.user.UserInfo;
import com.ruoyi.model.user.vo.UserInfoVo;
import com.ruoyi.ucenter.service.IUserInfoService;
import com.ruoyi.ucenter.service.IUserMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文章Controller
 *
 * @author ruoyi
 * @date 2024-04-03
 */
@Api(tags = "后台:文章接口")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/admin/blog")
public class BlogAdminController extends BaseController {
    @Autowired
    private IUserInfoService iUserInfoService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 消息服务
     */
    private final IUserMessageService messageService;

    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('system:blog:list')")
    @GetMapping("/list")
    @ApiOperation("文章列表")
    public TableDataInfo list(Blog blog) {
        startPage();
        System.out.println(blog.getTitle());
        List<Blog> list = blogService.selectWebBlogList(blog);
        list.forEach(item -> {
            UserInfo userInfo = iUserInfoService.getById(item.getPushId());
            item.setPushName(userInfo != null ? userInfo.getName() : null);
        });
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @Log(title = "导出文章列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Blog blog) {
        List<Blog> list = blogService.selectWebBlogList(blog);
        ExcelUtil<Blog> util = new ExcelUtil<Blog>(Blog.class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 删除文章
     */
    @Log(title = "删除文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(blogService.deleteWebBlogByIds(ids));
    }

    /**
     * 获取文章详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        // // 检查id是否为空或不合法
        // if (StringUtils.isEmpty(id) || !id.matches("^\\+?[1-9][0-9]*$")) {
        //     return AjaxResult.warn("id不合法");
        // }
        // UserInfo userInfo = AuthContextHolder.getUserInfo();
        // UserAndBlogVO userAndBlogVO = blogService.selectWebBlogById(id);
        // userInfo.setId(userAndBlogVO.getPushId());
        // UserInfoVo userInfoVo = iUserInfoService.selectUserInfo(userInfo);
        // userAndBlogVO= BeanCopyUtils.copyBean(userInfoVo, UserAndBlogVO.class);
        redisCache.incrementCacheMapValue(ActivitysConstant.BLOG_KEY, id, 1);
        Blog result = this.blogService.getById(id);
        String pushId = result.getPushId();
        UserInfo userInfo = this.iUserInfoService.getById(pushId);
        result.setPushName(userInfo != null ? userInfo.getName() : null);
        return success(result);
    }

    /**
     * 审核文章
     */
    @ApiOperation("审核文章信息")
    @GetMapping("/check/blog/{blogId}/{status}")
    public AjaxResult checkContest(@ApiParam("当前审核的竞赛id") @PathVariable("blogId")
                                       String blogId,
                                   @ApiParam("当前的审核状态") @PathVariable("status")
                                   String status) {
        return toAjax(this.blogService.checkBlogStatus(blogId, status));
    }

    /**
     * 审核完成后编辑消息发送消息
     */
    @ApiOperation("审核完成后编辑消息")
    @PostMapping("/send/message")
    public AjaxResult sendMessage(@ApiParam("发送的消息") @RequestBody UserMessage userMessage) {
        return toAjax(this.messageService.insertUserMessage(userMessage));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('system:blog:edit')")
    @Log(title = "修改文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Blog blog) {
        return toAjax(blogService.updateById(blog));
    }
}

